package com.wrench.elevator.base.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.wrench.elevator.base.dao.AppControllerDao;
import com.wrench.elevator.base.user.domain.User;
import com.wrench.elevator.base.user.service.ChengtiService;
import com.wrench.elevator.base.user.service.FaceService;
import com.wrench.elevator.base.user.service.PersonService;
import com.wrench.elevator.base.user.service.RechargeService;
import com.wrench.elevator.base.user.service.UserService;
import com.wrench.elevator.base.user.service.WalletService;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

@Controller
@RequestMapping("/app")
public class AppController implements AppControllerDao
{
    
    @Resource
    private UserService userService;
    
    @Resource
    private FaceService faceService;
    
    @Resource
    private WalletService walletService;
    
    @Resource
    private ChengtiService chengtiService;
    
    @Resource
    private RechargeService rechargeService;
    
    @Resource
    private PersonService personService;
    
    private static final Logger logger = LoggerFactory.getLogger(AppController.class);
    
    @RequestMapping("/login")
    @Override
    public void login(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.login()********** ");
        logger.debug("==> sessionId = " + request.getSession().getId());
        String loginType = WrenchUtil.getPara(request, "loginType");
        User user = null;
        WrenchException error = null;
        if (WrenchUtil.stringEquals(loginType, Dict.LOGIN_TYPE_BY_TELPHONE))
        {
            logger.debug("==> login by telphone , loginType = {} ", loginType);
            String telphone = WrenchUtil.getPara(request, "telphone");
            try
            {
                user = userService.loginByTelphone(telphone);
            }
            catch (WrenchException e)
            {
                // TODO Auto-generated catch block
                error = e;
            }
            
        }
        else if (WrenchUtil.stringEquals(loginType, Dict.LOGIN_TYPE_BY_WXID))
        {
            logger.debug("==> login by wxid , loginType = {} ", loginType);
            String wx_id = WrenchUtil.getPara(request, "wx_id");
            try
            {
                user = userService.loginByWxId(wx_id);
            }
            catch (WrenchException e)
            {
                // TODO Auto-generated catch block
                error = e;
            }
        }
        else
        {
            logger.error("==> loginType is not vaild , loginType = {} ", loginType);
            error =
                new WrenchException(Result.USER_LOGIN_TYPE_IS_NOT_VAILD_CODE,
                    Result.USER_LOGIN_TYPE_IS_NOT_VAILD_MESSAGE);
        }
        
        if (!WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        else if (WrenchUtil.hasNull(user))
        {
            map.put(Result.RESULT_CODE, Result.USER_HAS_NOT_REG_CODE);
            map.put(Result.RESULT_MESSAGE, Result.USER_HAS_NOT_REG_MESSAGE);
        }
        else
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("user", user);
        }
        
        WrenchUtil.responseMap(response, map);
        
        logger.debug("==> **********exit out AppController.login()********** ");
        
    }
    
    @RequestMapping("/reg")
    @Override
    public void reg(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.reg()********** ");
        String regType = WrenchUtil.getPara(request, "regType");
        WrenchException error = null;
        if (WrenchUtil.stringEquals(regType, Dict.REG_TYPE_BY_TELPHONE))
        {
            logger.debug("==> reg by telphone , regType = {} ", regType);
            String telphone = WrenchUtil.getPara(request, "telphone");
            String username = WrenchUtil.getPara(request, "username");
            String face_feature = WrenchUtil.getPara(request, "face_feature");
            String gender = WrenchUtil.getPara(request, "gender");
            String age = WrenchUtil.getPara(request, "age");
            
            try
            {
                userService.regUserByTelphone(telphone, username, face_feature, gender, age);
            }
            catch (WrenchException e)
            {
                error = e;
            }
            
        }
        else if (WrenchUtil.stringEquals(regType, Dict.REG_TYPE_BY_WXID))
        {
            logger.debug("==> reg by wxid , regType = {} ", regType);
            String wx_id = WrenchUtil.getPara(request, "wx_id");
            String username = WrenchUtil.getPara(request, "username");
            String face_feature = WrenchUtil.getPara(request, "face_feature");
            String gender = WrenchUtil.getPara(request, "gender");
            String age = WrenchUtil.getPara(request, "age");
            
            try
            {
                userService.regUserByWxId(wx_id, username, face_feature, gender, age);
            }
            catch (WrenchException e)
            {
                error = e;
                
            }
        }
        else
        {
            logger.error("==> regType is not vaild , regType = {} ", regType);
            error =
                new WrenchException(Result.USER_REG_TYPE_IS_NOT_VAILD_CODE, Result.USER_REG_TYPE_IS_NOT_VAILD_MESSAGE);
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        WrenchUtil.responseMap(response, map);
        logger.debug("==> **********exit out AppController.reg()********** ");
        
    }
    
    @RequestMapping("/addFace")
    @Override
    public void addFace(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.addFace()********** ");
        String face_feature = WrenchUtil.getPara(request, "face_feature");
        String gender = WrenchUtil.getPara(request, "gender");
        String age = WrenchUtil.getPara(request, "age");
        String user_id = WrenchUtil.getPara(request, "user_id");
        String identity = Dict.FACE_IDENTITY_FAMILY;
        int face_id = Dict.INIT_NEXT_FACE_ID;
        WrenchException error = null;
        try
        {
            face_id = faceService.getNextFaceId();
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (face_id != Dict.INIT_NEXT_FACE_ID)
        {
            try
            {
                faceService.addFace(user_id, String.valueOf(face_id), identity, face_feature, age, gender);
            }
            catch (WrenchException e)
            {
                // TODO Auto-generated catch block
                error = e;
            }
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        WrenchUtil.responseMap(response, map);
        
        logger.debug("==> **********exit out AppController.addFace()********** ");
    }
    
    @RequestMapping("/setAddress")
    @Override
    public void setAddress(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.setAddress()********** ");
        String user_id = WrenchUtil.getPara(request, "user_id");
        String province_id = WrenchUtil.getPara(request, "province_id");
        String province_name = WrenchUtil.getPara(request, "province_name");
        String city_id = WrenchUtil.getPara(request, "city_id");
        String city_name = WrenchUtil.getPara(request, "city_name");
        String district_id = WrenchUtil.getPara(request, "district_id");
        String district_name = WrenchUtil.getPara(request, "district_name");
        String street_name = WrenchUtil.getPara(request, "street_name");
        String building_id = WrenchUtil.getPara(request, "building_id");
        String building_name = WrenchUtil.getPara(request, "building_name");
        String zhuangshu = WrenchUtil.getPara(request, "zhuangshu");
        String danyuan = WrenchUtil.getPara(request, "danyuan");
        String loucen = WrenchUtil.getPara(request, "loucen");
        String menpaihao = WrenchUtil.getPara(request, "menpaihao");
        WrenchException error = null;
        try
        {
            userService.setAddress(user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        
        logger.debug("==> **********exit out AppController.setAddress()********** ");
    }
    
    @RequestMapping("/recharge")
    @Override
    public void recharge(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.recharge()********** ");
        String money = WrenchUtil.getPara(request, "money");
        String user_id = WrenchUtil.getPara(request, "user_id");
        WrenchException error = null;
        try
        {
            rechargeService.addRecharge(money, user_id);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        logger.debug("==> **********exit out AppController.recharge()********** ");
        
    }
    
    @RequestMapping("/chengti")
    @Override
    public void chengti(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.chengti()********** ");
        String face_id = WrenchUtil.getPara(request, "face_id");
        WrenchException error = null;
        try
        {
            chengtiService.chengti(face_id);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        logger.debug("==> **********exit out AppController.chengti()********** ");
        
    }
    
    @RequestMapping("/huti")
    @Override
    public void huti(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.huti()********** ");
        String user_id = WrenchUtil.getPara(request, "user_id");
        WrenchException error = null;
        try
        {
            chengtiService.huti(user_id);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        logger.debug("==> **********exit out AppController.huti()********** ");
        
    }
    
    @RequestMapping("/regByCloud")
    @Override
    public void regByCloud(HttpServletRequest request, HttpServletResponse response, ModelMap map,
        @RequestParam(value = "pic", required = true) MultipartFile pic)
    {
        logger.debug("==> **********enter in AppController.regByCloud()********** ");
        String regType = WrenchUtil.getPara(request, "regType");
        int flag = 0;
        WrenchException error = null;
        if (WrenchUtil.stringEquals(regType, Dict.REG_TYPE_BY_TELPHONE))
        {
            flag = 1;
            logger.debug("==> reg by telphone , regType = {} ", regType);
            String telphone = WrenchUtil.getPara(request, "telphone");
            String username = WrenchUtil.getPara(request, "username");
            String person_id = WrenchUtil.getPara(request, "person_id");
            
            try
            {
                userService.regUserByTelphoneCloud(telphone, username, person_id);
            }
            catch (WrenchException e)
            {
                error = e;
            }
            
        }
        else if (WrenchUtil.stringEquals(regType, Dict.REG_TYPE_BY_WXID))
        {
            flag = -1;
            logger.debug("==> reg by wxid , regType = {} ", regType);
            String wx_id = WrenchUtil.getPara(request, "wx_id");
            String username = WrenchUtil.getPara(request, "username");
            String person_id = WrenchUtil.getPara(request, "person_id");
            
            try
            {
                userService.regUserByWxIdCloud(wx_id, username, person_id);
            }
            catch (WrenchException e)
            {
                error = e;
                
            }
        }
        else
        {
            logger.error("==> regType is not vaild , regType = {} ", regType);
            error =
                new WrenchException(Result.USER_REG_TYPE_IS_NOT_VAILD_CODE, Result.USER_REG_TYPE_IS_NOT_VAILD_MESSAGE);
        }
        
        if (WrenchUtil.hasNull(error))
        {
            if (flag == 1)
            {
                WrenchUtil.save(pic, "/upload/", request, "TELPHONE" + WrenchUtil.getPara(request, "telphone")+Dict.CHENGTI_IDENTITY_MASTER);
            }
            else if (flag == -1)
            {
                WrenchUtil.save(pic, "/upload/", request, "WXID" + WrenchUtil.getPara(request, "wx_id")+Dict.CHENGTI_IDENTITY_FAMILY);
            }
            
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        logger.debug("==> **********exit out AppController.regByCloud()********** ");
    }
    
    @RequestMapping("/setAddressCloud")
    public void setAddressCloud(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.setAddressCloud()********** ");
        String user_id = WrenchUtil.getPara(request, "user_id");
        String province_id = WrenchUtil.getPara(request, "province_id");
        String province_name = WrenchUtil.getPara(request, "province_name");
        String city_id = WrenchUtil.getPara(request, "city_id");
        String city_name = WrenchUtil.getPara(request, "city_name");
        String district_id = WrenchUtil.getPara(request, "district_id");
        String district_name = WrenchUtil.getPara(request, "district_name");
        String street_name = WrenchUtil.getPara(request, "street_name");
        String building_id = WrenchUtil.getPara(request, "building_id");
        String building_name = WrenchUtil.getPara(request, "building_name");
        String zhuangshu = WrenchUtil.getPara(request, "zhuangshu");
        String danyuan = WrenchUtil.getPara(request, "danyuan");
        String loucen = WrenchUtil.getPara(request, "loucen");
        String menpaihao = WrenchUtil.getPara(request, "menpaihao");
        WrenchException error = null;
        try
        {
            userService.setAddressCloud(user_id,
                province_id,
                province_name,
                city_id,
                city_name,
                district_id,
                district_name,
                street_name,
                building_id,
                building_name,
                zhuangshu,
                danyuan,
                loucen,
                menpaihao);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        
        logger.debug("==> **********exit out AppController.setAddressCloud()********** ");
    }
    
    /**
     * 乘梯 人脸识别 云接口
     * */
    @RequestMapping("/chengtiCloud")
    public void chengtiCloud(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.chengtiCloud()********** ");
        WrenchException error = null;
        
        String person_id = WrenchUtil.getPara(request, "person_id");
        
       try
    {
        chengtiService.chengtiCloud(person_id);
    }
    catch (WrenchException e)
    {
        // TODO Auto-generated catch block
       error=e;
    }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        
        logger.debug("==> **********exit out AppController.chengtiCloud()********** ");
    }
    
    @RequestMapping("/hutiCloud")
    public void hutiCloud(HttpServletRequest request, HttpServletResponse response, ModelMap map)
    {
        logger.debug("==> **********enter in AppController.huti()********** ");
        String user_id = WrenchUtil.getPara(request, "user_id");
        WrenchException error = null;
        try
        {
            chengtiService.hutiCloud(user_id);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        
        WrenchUtil.responseMap(response, map);
        logger.debug("==> **********exit out AppController.huti()********** ");
        
    }
    
    @RequestMapping("/addFaceCloud")
    public void addFaceCloud(HttpServletRequest request, HttpServletResponse response, ModelMap map,@RequestParam(value = "pic", required = true) MultipartFile pic)
    {
        logger.debug("==> **********enter in AppController.addFaceCloud()********** ");
        String telphone = WrenchUtil.getPara(request, "telphone");
        String wx_id=WrenchUtil.getPara(request, "wx_id");
        String user_id = WrenchUtil.getPara(request, "user_id");
        String identity = Dict.FACE_IDENTITY_FAMILY;
        String person_id = WrenchUtil.getPara(request, "person_id");
        String input_time = WrenchUtil.nowTime();
        WrenchException error = null;
        
        try
        {
            personService.addPerson(user_id, person_id, identity, input_time);
        }
        catch (WrenchException e)
        {
            // TODO Auto-generated catch block
            error = e;
        }
        
        if (WrenchUtil.hasNull(error))
        {
            if (WrenchUtil.hasNull(wx_id))
            {
                WrenchUtil.save(pic, "/upload/", request, "TELPHONE" + WrenchUtil.getPara(request, "telphone")+Dict.CHENGTI_IDENTITY_FAMILY);
            }
            else 
            {
                WrenchUtil.save(pic, "/upload/", request, "WXID" + WrenchUtil.getPara(request, "wx_id")+Dict.CHENGTI_IDENTITY_FAMILY);
            }
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
        }
        else
        {
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        WrenchUtil.responseMap(response, map);
        
        logger.debug("==> **********exit out AppController.addFaceCloud()********** ");
    }
    
}
